package com.junzhiit.nengji.app.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.app.service.ShopHistoryService;
import com.junzhiit.nengji.app.web.exception.ChainNotFoundException;
import com.junzhiit.nengji.app.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.app.web.vo.ResultVO;
import com.junzhiit.nengji.app.web.vo.ShopAnalysisVO;
import com.junzhiit.nengji.app.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.bo.ShopAnalysisBO;
import com.junzhiit.nengji.bo.UserSexCountBO;
import com.junzhiit.nengji.dao.ChainDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.ShopHistoryDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Chain;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.enums.Is;

@Service
public class ShopHistoryServiceImpl extends BaseServiceImpl implements ShopHistoryService {

	
	@Autowired
	private ShopHistoryDAO shopHistoryDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;
	
	@Autowired
	private ChainDAO chainDAO;
	
	@Autowired
	private ShopDAO shopDAO;

	@Override
	public ResultVO getShopAnalysis(String sessionId, String startTime, String endTime, String shopId) {
		ResultVO result=new ResultVO();
		SystemUserSessionVO sessionVO=systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		String systemId=sessionVO.getSystemId();
		Integer type=0;
		if(sessionVO.getIsChain()==Is.NO.toIntValue()){
			Shop shop=shopDAO.query(systemId);
			if(shop==null){
				throw new ShopNotFoundException("店铺不存在");
			}
			type=1;
		}
		if(sessionVO.getIsChain()==Is.YES.toIntValue()){
			Chain chain=chainDAO.query(systemId);
			if(chain==null){
				throw new ChainNotFoundException("品牌不存在");
			}
			if(StringUtils.isNoneBlank(shopId)){
				type=1;
				systemId=shopId;
			}	
		}
		Date sTime=DateUtil.getDateStart(DateUtil.parseDate(startTime));
		Date eTime=DateUtil.getDateEnd(DateUtil.parseDate(endTime));
		ShopAnalysisBO sab=shopHistoryDAO.getShopAnalysis(sTime, eTime, systemId, type);
		ShopAnalysisVO sav=new ShopAnalysisVO();
		BeanUtils.copyProperties(sab, sav);
		sav.setShopViewNumber(sav.getShopViewNumber()==null?0:sav.getShopViewNumber());
		sav.setShopShareNumber(sav.getShopShareNumber()==null?0:sav.getShopShareNumber());
		result.setData(sav);
		return result;
	}

	@Override
	public ResultVO getUserSex(String sessionId, String startTime, String endTime, String shopId) {
		ResultVO result=new ResultVO();
		SystemUserSessionVO sessionVO=systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		String systemId=sessionVO.getSystemId();
		Integer type=0;
		if(sessionVO.getIsChain()==Is.NO.toIntValue()){
			Shop shop=shopDAO.query(systemId);
			if(shop==null){
				throw new ShopNotFoundException("店铺不存在");
			}
			type=1;
		}
		if(sessionVO.getIsChain()==Is.YES.toIntValue()){
			Chain chain=chainDAO.query(systemId);
			if(chain==null){
				throw new ChainNotFoundException("品牌不存在");
			}
			if(StringUtils.isNoneBlank(shopId)){
				type=1;
				systemId=shopId;
			}	
		}
		Date sTime=DateUtil.getDateStart(DateUtil.parseDate(startTime));
		Date eTime=DateUtil.getDateEnd(DateUtil.parseDate(endTime));
		UserSexCountBO uscb=shopHistoryDAO.getUserSex(sTime, eTime, systemId,type);
		result.setData(uscb);
		return result;
	}

}
